package com.holyn.guofang.viewcommon.popupwindow;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.animation.Animation;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.holyn.guofang.R;

import java.util.List;

import razerdp.basepopup.BasePopupWindow;

/**
 * Created by Holyn on 2017/7/14.
 */

public class PopupSingleSelectFromBottom extends BasePopupWindow{

    private List<String> itemList;
    private MyRecyclerAdapter recyclerAdapter;
    private RecyclerView recyclerView;

    private String title;
    private TextView tvTitle;

    private OnItemClickListener onItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(String item, int position);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public PopupSingleSelectFromBottom(Activity context, String title, List<String> itemList) {
        super(context);
        this.title = title;
        this.itemList = itemList;
        initView();
    }

    @Override
    public View onCreatePopupView() {
        return createPopupById(R.layout.popup_single_select_from_bottom);
    }

    @Override
    public View initAnimaView() {
        return getPopupWindowView().findViewById(R.id.ll_anima_view);
    }

    @Override
    protected Animation initShowAnimation() {
        return getTranslateAnimation(250 * 2, 0, 300);
    }


    @Override
    public View getClickToDismissView() {
        return getPopupWindowView().findViewById(R.id.ll_click_to_dismiss);
    }

    private void initView() {
        tvTitle = (TextView) getPopupWindowView().findViewById(R.id.tv_title);
        tvTitle.setText(title);

        recyclerAdapter = new MyRecyclerAdapter(itemList);
        recyclerView = (RecyclerView) getPopupWindowView().findViewById(R.id.rv_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.addItemDecoration(new MyDivider(getContext()));

        recyclerAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                onItemClickListener.onItemClick(itemList.get(position), position);
                dismiss();
            }
        });

        recyclerView.setAdapter(recyclerAdapter);
    }

    private class MyRecyclerAdapter extends BaseQuickAdapter<String, BaseViewHolder>{

        public MyRecyclerAdapter(@Nullable List<String> itemList) {
            super(R.layout.item_text, itemList);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            helper.setText(R.id.tv_content, item);
        }
    }

    private class MyDivider extends RecyclerView.ItemDecoration {
        private Drawable mDivider;
        private int mDividerHdeght = 1;
        private final int[] ATTRS = new int[]{android.R.attr.listDivider};

        public MyDivider(Context context) {
            TypedArray a = context.obtainStyledAttributes(ATTRS);
            this.mDivider = a.getDrawable(0);
            a.recycle();
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
            drawHorizontalLine(c, parent, state);
        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            super.getItemOffsets(outRect, view, parent, state);
            outRect.set(0, 0, 0, mDividerHdeght);
        }

        //画横线, 这里的parent其实是显示在屏幕显示的这部分
        public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state){
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();
            final int childCount = parent.getChildCount();
            for (int i = 0; i < childCount; i++){
                final View child = parent.getChildAt(i);

                //获得child的布局信息
                final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams)child.getLayoutParams();
                final int top = child.getBottom() + params.bottomMargin;
                final int bottom = top + mDivider.getIntrinsicHeight();
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }
}
